package com.zj.web.controller;

import com.zj.service.OrdersService;
import com.zj.web.bean.OrderMessageDTO;
import com.zj.web.bean.OrdersDTO;
import com.zj.web.bean.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/auth/orders")
@Tag(name = "订单管理",description = "实现对订单的增删改查工作")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    //添加订单
    @PostMapping("/add")
    @Operation(summary = "添加订单",description = "根据用户id和地址id添加订单")
    public ResponseEntity<ResponseResult> addOrder(@RequestParam("userId") Long userId,
                                                   @RequestParam("addressId") Long addressId){
        OrdersDTO orders = new OrdersDTO();
        orders.setUserId(userId);
        orders.setAddressId(addressId);
        ordersService.addOrder(orders);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("添加成功").data(orders).build());
    }

    //多表查询未完成的订单
    @GetMapping("/findUnfinishedOrder/{orderStatus}")
    @Operation(summary = "多表查询未完成的订单",description = "根据订单状态和用户Id查询未完成的订单")
    public ResponseEntity<ResponseResult> findUnfinishedOrder(@PathVariable("orderStatus") Integer orderStatus,
                                                              @RequestParam(value = "userId", required = false) Long userId) { // 改为非必需参数
        List<OrdersDTO> orderdetailList = ordersService.findUnfinishedOrder(orderStatus,userId);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(orderdetailList).build());
    }

    //根据订单id修改订单状态
    @PutMapping("/updateOrderStatus/{orderId}/{orderStatus}")
    @Operation(summary = "根据订单id修改订单状态",description = "根据订单id修改订单状态")
    public ResponseEntity<ResponseResult> updateOrderStatus(@PathVariable("orderId") Long orderId,
                                                             @PathVariable("orderStatus") Integer orderStatus,
                                                             @RequestParam("email") String email){
        boolean result = ordersService.updateOrderStatus(orderId, orderStatus,email);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("修改成功").data(result).build());
    }

    //根据id查询订单信息，多表查询
    @GetMapping("/findOrderById/{orderId}")
    @Operation(summary = "根据订单id查询订单详情",description = "根据订单id查询订单详情")
    public ResponseEntity<ResponseResult> findOrderById(@PathVariable("orderId") Long orderId){
        List<OrdersDTO> orders = ordersService.findOrderById(orderId);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(orders).build());
    }
    //添加订单（AI客服下单）
    @PostMapping("/addByAI")
    @Operation(summary = "添加订单（AI客服下单）",description = "根据用户id和地址id添加订单（AI客服下单）")
    public ResponseEntity<ResponseResult> addOrderByAI(@RequestParam("userId") Long userId,
                                                   @RequestParam("addressId") Long addressId){
        OrdersDTO orders = new OrdersDTO();
        orders.setUserId(userId);
        orders.setAddressId(addressId);
        OrderMessageDTO orderMessageDTO = ordersService.addOrderByAI(orders);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("添加成功").data(orderMessageDTO).build());
    }
    //根据订单id查询订单状态
    @GetMapping("/findOrderStatusById/{orderId}")
    @Operation(summary = "根据订单id查询订单状态",description = "根据订单id查询订单状态")
    public ResponseEntity<ResponseResult> findOrderStatusById(@PathVariable("orderId") Long orderId){
        Integer orderStatus = ordersService.findOrderStatusById(orderId);
        if (orderStatus == null) {
            return ResponseEntity.ok(ResponseResult.builder().code(0).msg("订单不存在").build());
        }
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(orderStatus).build());
    }
}
